package com.sk.weichat.ui.me.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.bean.message.ChatMessage;
import com.sk.weichat.broadcast.MsgBroadcast;
import com.sk.weichat.helper.DialogHelper;
import com.sk.weichat.pay.sk.AlipayAuth;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.me.redpacket.SendRedPacketActivity;
import com.sk.weichat.ui.me.wallet.view.AliPayView;
import com.sk.weichat.ui.me.wallet.view.AuthCallbackView;
import com.sk.weichat.ui.me.wallet.viewmode.AliPayViewMode;
import com.sk.weichat.ui.message.ChatActivity;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.SelectionFrame;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import de.greenrobot.event.EventBus;

/**
 * 身份验证
 */
public class IdentityAuthenticationActivity extends BaseActivity implements View.OnClickListener, AliPayView, AuthCallbackView {

    private ImageView ivBack;
    private TextView tv_title_center;
    private LinearLayout ll_alipay_bind;
    private TextView tv_alipay_nickname;
    private TextView tv_alipay_hint;
    private Button btn_bind;
    private AliPayViewMode mAliPayViewMode;
    private String aliPayNickName;
    private boolean isRedpacket;//判断是否是发红包 时 绑定
    private boolean isReceiveRedpack;//判断是否是领取红包是判断绑定
    private ChatMessage message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_identity_authentication);
        getSupportActionBar().hide();
        initView();
        initData();
    }

    private void initView() {
        ivBack = findViewById(R.id.iv_title_left);
        tv_title_center = findViewById(R.id.tv_title_center);
        tv_title_center.setText(getString(R.string.alipay));
        ll_alipay_bind = findViewById(R.id.ll_alipay_bind);
        tv_alipay_nickname = findViewById(R.id.tv_alipay_nickname);
        tv_alipay_hint = findViewById(R.id.tv_alipay_hint);
        btn_bind = findViewById(R.id.btn_bind);
        ivBack.setOnClickListener(this);
        btn_bind.setOnClickListener(this);
        mAliPayViewMode = new AliPayViewMode(coreManager, this);
    }

    private void initData() {
        aliPayNickName = getIntent().getStringExtra("aliPayNickName");
        isRedpacket = getIntent().getBooleanExtra("isRedpacket",false);
        isReceiveRedpack = getIntent().getBooleanExtra("isReceiveRedpack",false);
        if (isReceiveRedpack){
            message = getIntent().getParcelableExtra("ChatMessage");
        }
        if (TextUtils.isEmpty(aliPayNickName)) {//空表示没有绑定支付宝
            ll_alipay_bind.setVisibility(View.GONE);
            tv_alipay_hint.setText(getResources().getText(R.string.unbind_alipay_hint));
            btn_bind.setText(getString(R.string.alipay_bind));
        } else {
            ll_alipay_bind.setVisibility(View.VISIBLE);
            tv_alipay_nickname.setText(aliPayNickName);
            tv_alipay_hint.setText(getResources().getText(R.string.bind_alipay_hint));
            btn_bind.setText(getString(R.string.unbind));
        }
    }

    @Override
    public void onClick(View v) {
        if (v == ivBack) {
            finish();
        } else if (v == btn_bind) {
            if (TextUtils.isEmpty(aliPayNickName)) {//没绑定  授权
                mAliPayViewMode.getAlipayAutoMsg();
            } else {//解绑
                SelectionFrame setPasswordDialog = new SelectionFrame(this);
                setPasswordDialog.setSomething(getString(R.string.dialog_title), getString(R.string.unbind_alipay_dialog), getString(R.string.cancel), getString(R.string.sure), new SelectionFrame.OnSelectionFrameClickListener() {
                    @Override
                    public void cancelClick() {

                    }

                    @Override
                    public void confirmClick() {
                        mAliPayViewMode.UnBindAlipay();
                    }
                });
                setPasswordDialog.show();
            }
        }
    }


    @Override
    public void AlipayAuthMsgResult(ObjectResult<String> result) {
        if (result.getResultCode() == 1) {
            AlipayAuth alipayAuth = new AlipayAuth(this);
            alipayAuth.callAuth(this, result.getData(),true);
        } else {
            ToastUtil.showToast(this, result.getResultMsg());
        }
    }

    @Override
    public void AlipayAutoCodeResult(ObjectResult<String> result) {
        if (result.getResultCode() == 1) {
            if (isRedpacket){
                EventBus.getDefault().post("bind_success");
                finish();
            }else if (isReceiveRedpack){
                MsgBroadcast.broadcastMsgBindAlipayUpdate(mContext,message);
//                EventBus.getDefault().post(message);
                finish();
            }else {
                EventBus.getDefault().post("bind_success");
                ToastUtil.showToast(this, R.string.bind_success);
                aliPayNickName = result.getData();
                ll_alipay_bind.setVisibility(View.VISIBLE);
                tv_alipay_nickname.setText(aliPayNickName);
                tv_alipay_hint.setText(getResources().getText(R.string.bind_alipay_hint));
                btn_bind.setText(getString(R.string.unbind));
            }
        } else {
            ToastUtil.showToast(this, result.getResultMsg());
        }
    }

    @Override
    public void AlipayUnbindResult(ObjectResult<String> result) {
        if (result.getResultCode() == 1) {
            ToastUtil.showToast(this, R.string.unbind_success);
            aliPayNickName = "";
            ll_alipay_bind.setVisibility(View.GONE);
            tv_alipay_hint.setText(getResources().getText(R.string.unbind_alipay_hint));
            btn_bind.setText(getString(R.string.alipay_bind));
            EventBus.getDefault().post("unbind_success");
        } else {
            ToastUtil.showToast(this, result.getResultMsg());
        }
    }

    /**
     * 支付宝授权成功返回
     */
    @Override
    public void AlipayAuthCallbackResult(String authCode) {
        if (TextUtils.isEmpty(authCode)) {
            showLoadFailMsg(getString(R.string.alipay_auth_cancle));
        } else {
            mAliPayViewMode.setAlipayAutoCode(authCode);
        }
    }

    @Override
    public void showProgress() {
        if (!DialogHelper.isShowing()) {
            DialogHelper.showDefaulteMessageProgressDialog(this);
        }
    }

    @Override
    public void hideProgress() {
        DialogHelper.dismissProgressDialog();
    }

    @Override
    public void showLoadFailMsg(String err) {
        ToastUtil.showToast(this, err);
    }


}
